package com.thjk.bdf.preloan.service.controller;

import com.thjk.bdf.common.base.BasicResp;
import com.thjk.bdf.common.enmus.ServiceEnum;
import com.thjk.bdf.common.entity.BdfFundingParty;
import com.thjk.bdf.common.entity.BdfPayOut;
import com.thjk.bdf.common.request.PayoutNotifyRequest;
import com.thjk.bdf.preloan.service.repository.dao.BdfFundingPartyMapper;
import com.thjk.bdf.preloan.service.repository.dao.BdfPayOutMapper;

import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/payout/")
public class PayoutNotifyController {

    private static final Logger logger = LoggerFactory.getLogger(PayoutNotifyController.class);

    @Autowired
    private  BdfPayOutMapper payOutMapper;
    @Autowired
    private BdfFundingPartyMapper fundingPartyMapper;

	@RequestMapping(value = "notify", method = RequestMethod.POST)
    public BasicResp payoutNotify(@RequestBody PayoutNotifyRequest request)  {
    	logger.info("请求参数："+ request.toString());
    	String eventType = request.getEventType();
    	try {
			if(ServiceEnum.PUT_OUT_RESULT.getCode().equals(eventType)){
            	Map<String,String> map=(Map<String, String>) request.getData();
				BdfPayOut record = new BdfPayOut();
				BeanUtils.copyProperties(record, map);
				//先查询是否存在存在更新否则插入
				//if(payOutMapper.)
				if(null != payOutMapper.selectByPutoutNo(map.get("putoutNo"))) {
					payOutMapper.updateByPutoutNoSelective(record);
				}else {
					payOutMapper.insertSelective(record);
				}	
			}else if(ServiceEnum.ALTERNATE_FUNDS_KEY.getCode().equals(eventType)){
				BdfFundingParty record = new BdfFundingParty();
				BeanUtils.copyProperties(record, request.getData());
				fundingPartyMapper.insertSelective(record);
			}else{
				return BasicResp.back(ServiceEnum.SERVICE_000006.getCode(), ServiceEnum.SERVICE_000006.getDesc());
			}
			return BasicResp.success();
		} catch (Exception e) {
			logger.error("error：",e);
			return BasicResp.error();
		} 
    }
}
